package com.dc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dc.common.ResponseResultVO;
import com.dc.entity.ro.DictionaryRO;
import com.dc.mapper.DictionaryMapper;
import com.dc.model.Dictionary;
import com.dc.service.DictionaryService;
import com.dc.util.SnowflakeIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dc.common.ResponseResultVO.*;

@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Resource
    DictionaryMapper dictionaryMapper;

    @Override
    public ResponseResultVO selectDictionaryList(DictionaryRO dictionaryRO) {
        checkParameterIsNotNull(dictionaryRO.getType(), "type不能为空");
        List<Dictionary> list = dictionaryMapper.selectList(new QueryWrapper<Dictionary>().eq("type", dictionaryRO.getType()).eq(
                "is_delete", 0).orderByDesc(
                "create_time"));
        return ResponseResultVO.successResult(list);
    }

    @Override
    public ResponseResultVO insertDictionary(DictionaryRO dictionaryRO) {
        checkParameterIsNotNull(dictionaryRO.getType(), "type不能为空");
        checkParameterIsNotNull(dictionaryRO.getValue(), "值不能为空");
        Dictionary dto = new Dictionary();
        BeanUtils.copyProperties(dictionaryRO, dto);
        dto.setId(SnowflakeIdWorker.generateId());
        return insertResult(dictionaryMapper.insert(dto));
    }

    @Override
    public ResponseResultVO updateDictionary(DictionaryRO dictionaryRO) {
        checkParameterIsNotNull(String.valueOf(dictionaryRO.getId()), "id不能为空");
        Dictionary dto = new Dictionary();
        BeanUtils.copyProperties(dictionaryRO, dto);
        return updateResult(dictionaryMapper.updateById(dto));
    }

    @Override
    public ResponseResultVO deleteDictionaryById(String id) {
        checkParameterIsNotNull(id, "id不能为空");
        Dictionary dto = new Dictionary();
        dto.setId(Long.valueOf(id));
        dto.setIsDelete(1);
        return deleteResult(dictionaryMapper.updateById(dto));
    }

    @Override
    public ResponseResultVO selectAllDictionaryList() {
        List<Dictionary> list = dictionaryMapper.selectList(new QueryWrapper<Dictionary>().eq(
                "is_delete", 0).orderByDesc(
                "create_time"));
        Map<String, Object> resultMap = new HashMap<>();
        for (Dictionary dictionary : list) {
            if (resultMap.containsKey(dictionary.getType())) {
                List list1 = (ArrayList) resultMap.get(dictionary.getType());
                list1.add(dictionary);
            } else {
                List<Dictionary> dtos = new ArrayList<>();
                dtos.add(dictionary);
                resultMap.put(dictionary.getType(), dtos);
            }
        }

        return ResponseResultVO.successResult(resultMap);
    }

    @Override
    public ResponseResultVO selectDictionaryByType(String type) {
        checkParameterIsNotNull(type, "type不能为空");
        List<Dictionary> list = dictionaryMapper.selectList(new QueryWrapper<Dictionary>().eq("type", type).eq(
                "is_delete", 0).orderByDesc(
                "create_time"));
        if (CollectionUtils.isEmpty(list)) {
            return ResponseResultVO.failResult("当前无配置");
        }
        return ResponseResultVO.successResult(list);
    }
}
